package unipi.helpers;

import java.util.List;
import java.util.Map;

public class HTMLPages
{	
	public static String getGeneralPage(String contentHTML, String script, String[] sidebarHTML)
	{
		String html = HTMLParts.DOCTYPE + "\n<html xmlns=\"http://www.w3.org/1999/xhtml\">";
		
		html += HTMLParts.getHead(HTMLParts.TITLE, HTMLParts.CSSMOSTPAGES, script);
		
		html += "<body>\n" +
				HTMLParts.LOGOANDMENU +
				HTMLParts.PAGESTART +
				HTMLParts.CONTENTSTART + 
				contentHTML +
				HTMLParts.CONTENTEND;; 
		
		for(String sidebar : sidebarHTML)
		{
			if(sidebar == null) continue;
			html += HTMLParts.SIDESTART;			
			html += sidebar;			
			html += HTMLParts.SIDEEND;
		}		
		
		
		html += HTMLParts.PAGEEND;
		
		html += HTMLParts.FOOTER;
		html += "</body></html>";
		return html;		
	}
	
	public static String getDropDownList(List<List<String>> data, String name, String selection, String none, String extras)
	{
		String html = "<select name=\"" + name + "\"" + extras + ">";		
		if(none != null) html += "<option value=\"none\">" + none + "</option>";
		
		if(data != null)
		{
			for(List<String> row : data)
			{
				String selected = "";
				if(selection != null)
				{					
					if(row.get(0).equals(selection))
						selected = " selected=\"selected\"";
				}
				
				html += "<option class=\"sel\" value=\"" + row.get(0) + "\"" + selected + "><div class=\"sel\">" + row.get(1) + "</div></option>";				
			}
			
		}
		
		html += "</select>";
		
		return html;		
	}
	
	public static String getDropDownList(List<List<String>> data, String name, String selection, String none)
	{
		return getDropDownList(data, name, selection, none, "");		
	}
	
	public static String getGeneralPage(String contentHTML, String userID)
	{		
		return getGeneralPage(contentHTML, "", Sidebar.getSidebarHTML(userID));		
	}
	
	public static String getGeneralPage(String contentHTML, String script, String userID)
	{		
		return getGeneralPage(contentHTML, script, Sidebar.getSidebarHTML(userID));		
	}
	
	public static String getSubmitForm(String action, String[] titles, String[] inputs, String submitBtn, String formExtras)
	{
		if(titles.length != inputs.length) return "";
		
		String html = "<form action=\"" + action + "\" method=\"post\" " + formExtras + ">";
		
		html += "<table>";
		
		int rows = inputs.length;
		
		for(int x = 0; x < rows; x++)
		{
			html += "<tr><td>" + titles[x] + "</td>";
			html += "<td>" + inputs[x] + "</td></tr>";			
		}
		
		html += "<tr><td></td><td><input class=\"longbutton\" type=\"submit\" value=\"" + submitBtn + "\" /></td></tr>";
		html += "</table></form>";		
		
		return html;	
	}

	public static String getSubmitForm(String action, String[] titles, String[] inputs, String submitBtn)
	{
		return getSubmitForm(action, titles, inputs, submitBtn, "");
	}
	
	public static String getCalendarWeek(int startDay, int endDay, int offset, Map<Integer, String[]> dayData)
	{		
		String html = "<table id=\"calendarWeek\"><thead>";
		
		html += "<tr><td>Δευτέρα</td><td>Τρίτη</td><td>Τετάρτη</td><td>Πέμπτη</td><td>Παρασκευή</td><td>Σάββατο</td><td>Κυριακή</td>" +
				"</tr><tr class=\"fillerRow\"><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></thead>";
		
		html += "<tfoot><tr><td></td><td></td><td></td><td></td><td></td><td></td>" +
				"<td class=\"lastCol\"></td></tr></tfoot><tbody>";
		
		for(int week = startDay; week <= Math.ceil((endDay+offset) / 7.0); week++)
		{
			html += "<tr class=\"dates\">"; 			
			for(int day = (week*7) - 6 - offset; day < week*7 + 1 - offset; day++)
			{			
				html += "<td>" + (day>endDay?(day % endDay):(day<=0?"":day)) + "</td>";
			}			
			html += "</tr>";
			
			if(dayData != null)
			{
				html += "<tr>"; 			
				for(int day = (week*7) - 6 - offset; day < week*7 + 1 - offset; day++)
				{	
					String lastCol = (day == week*7-offset)?"class=\"lastCol\"":"";
					
					if(dayData.containsKey(day))
					{
						html += "<td " + lastCol + "><ul class=\"sideList\">";
								
						for(String s : dayData.get(day))
						{
							html += "<li>" + s + "</li>";							
						}
								
						html += "</ul></td>";
					}
					else
						html += "<td " + lastCol + "></td>"; 
				}			
				html += "</tr>";
			}
		}
		
		html += "</tbody></table>";
		
		return html;	
	}
	
}
